package com.xcyy.zjdxmjk.zjdxmjk.controller;

import com.xcyy.zjdxmjk.zjdxmjk.VO.CountryVO;
import com.xcyy.zjdxmjk.zjdxmjk.VO.ResultVO;
import com.xcyy.zjdxmjk.zjdxmjk.VO.XzqVO;
import com.xcyy.zjdxmjk.zjdxmjk.common.ResultVOUtil;
import com.xcyy.zjdxmjk.zjdxmjk.entity.City;
import com.xcyy.zjdxmjk.zjdxmjk.entity.Country;
import com.xcyy.zjdxmjk.zjdxmjk.entity.Town;
import com.xcyy.zjdxmjk.zjdxmjk.entity.UserInfo;
import com.xcyy.zjdxmjk.zjdxmjk.service.CityService;
import com.xcyy.zjdxmjk.zjdxmjk.service.CountryService;
import com.xcyy.zjdxmjk.zjdxmjk.service.TownService;
import com.xcyy.zjdxmjk.zjdxmjk.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
public class CountryController {
    @Autowired
    private CountryService countryService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private TownService townService;
    @Autowired
    private CityService cityService;

    @PostMapping("/addcountry")
    private ResultVO addCountry(Country country) {
        Country result = countryService.save(country);
        if (result == null) {
            return ResultVOUtil.error(400, "添加失败");
        }
        return ResultVOUtil.success(result);
    }

    @PostMapping("/editcountry")
    private ResultVO editcountry(String code, boolean ssfa, Integer sjjk, Integer nyjc, Integer wyjc,Integer sxlr,Integer yhyddy) {
        Country country = countryService.findByCode(code);
        if (country == null) {
            return ResultVOUtil.error(400, "没有找到code");
        }
        country.setSsfa(ssfa);
        country.setSjjk(sjjk);
        country.setNyjc(nyjc);
        country.setWyjc(wyjc);
        country.setSxlr(sxlr);
        country.setYhyddy(yhyddy);
        Country result = countryService.save(country);
        return ResultVOUtil.success(result);
    }

    @PostMapping("/findmycountries")
    private ResultVO findmycountries(String userid) {

        UserInfo userInfo = userInfoService.findByUserid(userid);
        if (userInfo == null) {
            return ResultVOUtil.error(400, "没有找到该用户");
        }
        if (!"1".equals(userInfo.getPosition())) {
            return ResultVOUtil.error(401, "你没有录入权限");
        }

        List<CountryVO> countryVOS = new ArrayList<>();
        CountryVO countryVO;
        List<Country> countries = countryService.findByDept(userInfo.getDepartment());
        for (Country country : countries) {

            countryVO = new CountryVO();
            BeanUtils.copyProperties(country, countryVO);
            if(country.isSsfa()){
                countryVO.setSsfa("1");
            }else {
                countryVO.setSsfa("0");
            }
            countryVOS.add(countryVO);
        }
        return ResultVOUtil.success(countryVOS);
    }

    @PostMapping("/calcount")
    private String calcount() {
        //计算区县
        List<Object> objects = townService.countZjdAndJsydGroupByCountry();
        Country country;
        for (Object o : objects) {
            country = countryService.findByCode((((Object[]) o)[0]).toString());
            if (country == null) {
                continue;
            }
            country.setZjdCount(Integer.parseInt(((BigDecimal) ((Object[]) o)[1]).toString()));
            country.setJsydCount(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
            countryService.save(country);
        }
        //计算市
        List<Object> os = townService.countZjdAndJsydGroupByCity();
        City city;
        for (Object o : os) {
            city = cityService.findByCode((((Object[]) o)[0]).toString());
            if (city == null) {
                continue;
            }
            city.setZjdCount(Integer.parseInt(((BigDecimal) ((Object[]) o)[1]).toString()));
            city.setJsydCount(Integer.parseInt(((BigDecimal) ((Object[]) o)[2]).toString()));
            cityService.save(city);
        }
        return "ok";
    }
}
